package com.ruoyi.web.controller.biz;

import com.ruoyi.biz.domain.BizOrder;
import com.ruoyi.biz.domain.vo.BizOrderVO;
import com.ruoyi.biz.service.IBizOrderService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 订单Controller
 * 
 * @author ruoyi
 * @date 2022-02-18
 */
@RestController
@RequestMapping("/biz/order")
public class BizOrderController extends BaseController
{
    @Autowired
    private IBizOrderService bizOrderService;

    /**
     * 查询订单列表
     */
    @GetMapping("/list")
    public TableDataInfo list(BizOrder bizOrder)
    {
        startPage();
        List<BizOrderVO> list = bizOrderService.selectBizOrderVOList(bizOrder);
        return getDataTable(list);
    }

    /**
     * 导出订单列表
     */
    @Log(title = "订单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BizOrder bizOrder)
    {
        List<BizOrder> list = bizOrderService.selectBizOrderList(bizOrder);
        ExcelUtil<BizOrder> util = new ExcelUtil<BizOrder>(BizOrder.class);
        util.exportExcel(response, list, "订单数据");
    }

    /**
     * 获取订单详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(bizOrderService.selectBizOrderById(id));
    }

    /**
     * 获取订单详细信息
     */
    @GetMapping(value = "/detail/{id}")
    public AjaxResult getDetailById(@PathVariable("id") Long id)
    {
        return AjaxResult.success(bizOrderService.selectBizOrderVOById(id));
    }

    /**
     * 新增订单
     */
    @Log(title = "订单", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public AjaxResult add(BizOrder bizOrder)
    {
        return toAjax(bizOrderService.insertBizOrder(bizOrder));
    }

    /**
     * 创建订单
     */
    @Log(title = "订单", businessType = BusinessType.INSERT)
    @PostMapping("/create")
    public AjaxResult create(BizOrder bizOrder, String parcelIds)
    {
        return AjaxResult.success(bizOrderService.createBizOrder(bizOrder, parcelIds));
    }

    /**
     * 修改订单并唤起支付链接
     */
    @Log(title = "修改订单并唤起支付链接", businessType = BusinessType.UPDATE)
    @PostMapping("/pay/start")
    public AjaxResult startPay(BizOrder bizOrder)
    {
        return AjaxResult.success(bizOrderService.startPay(bizOrder));
    }

    /**
     * 修改订单并唤起支付链接
     */
    @Log(title = "根据token查询支付状态")
    @PostMapping("/pay/transaction")
    public AjaxResult transaction(String token)
    {
        return AjaxResult.success(bizOrderService.getTransaction(token));
    }

    /**
     * 修改订单
     */
    @Log(title = "订单", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    public AjaxResult edit(@RequestBody BizOrder bizOrder)
    {
        return toAjax(bizOrderService.updateBizOrder(bizOrder));
    }

    /**
     * 删除订单
     */
    @Log(title = "订单", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(bizOrderService.deleteBizOrderByIds(ids));
    }
}
